package com.example.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.common.BaseContext;
import com.example.common.R;
import com.example.domain.entity.ShoppingCart;
import com.example.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 * @author xiaoduan
 * @since 2024-09-24
 */
@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private IShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public R list() {
        log.info("查看购物车...");

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }

    // 添加购物车

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("添加到购物车...");
        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 判断购物车是否已经存在 存在加一
        ShoppingCart one = shoppingCartService.getOne(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getUserId, BaseContext.getCurrentId()).eq(ObjectUtils.isNotEmpty(shoppingCart.getDishId()), ShoppingCart::getDishId, shoppingCart.getDishId()).eq(ObjectUtils.isNotEmpty(shoppingCart.getSetmealId()), ShoppingCart::getSetmealId, shoppingCart.getSetmealId()));
        if (one != null) {
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);
        } else {
            shoppingCart.setCreateTime(DateUtil.now());
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }
        return R.success(one);
    }

    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart) {
        log.info("减少购物车...");
        // 判断购物车是否存在 存在减一
        ShoppingCart one = shoppingCartService.getOne(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getUserId, BaseContext.getCurrentId()).eq(ObjectUtils.isNotEmpty(shoppingCart.getDishId()), ShoppingCart::getDishId, shoppingCart.getDishId()).eq(ObjectUtils.isNotEmpty(shoppingCart.getSetmealId()), ShoppingCart::getSetmealId, shoppingCart.getSetmealId()));
        if (one != null && one.getNumber() > 1) {
            one.setNumber(one.getNumber() - 1);
            shoppingCartService.updateById(one);
        } else {
            shoppingCartService.removeById(one);
        }
        return R.success("delete successfully");
    }

    @DeleteMapping("/clean")
    public R clean() {
        log.info("清空购物车...");
        shoppingCartService.remove(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getUserId, BaseContext.getCurrentId()));
        return R.success("clean successfully");
    }

}